﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;

public class RequestHelper
{
    private static HttpWebRequest GetRequest(string url, string reference, string cookie)
    {
        try
        {
            var uri = new Uri(url);
            var req = (HttpWebRequest)WebRequest.Create(uri);
            req.Accept =
                "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/x-shockwave-flash, application/vnd.ms-powerpoint, */*";
            req.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-cn");
            if (reference != "")
            {
                req.Referer = reference;
            }
            else
            {
                req.Referer = "http://" + uri.Host;
            }
            req.Headers.Add("UA-CPU:x86");
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;)";
            req.KeepAlive = true;
            if (cookie != "")
            {
                req.Headers.Add(HttpRequestHeader.Cookie, cookie);
            }
            req.Timeout = 10000;
            return req;
        }
        catch
        {
            return null;
        }
    }

    private static byte[] HttpDecompress(HttpWebResponse resp)
    {
        var offset = 0;
        byte[] deBuffer;
        if (resp == null)
            return null;
        Stream s = null;
        try
        {
            s = resp.GetResponseStream();
            var zipBuffer = new byte[1024 * 1024];
            var bytesRead = 0;
            while (true)
            {
                if (s != null) bytesRead = s.Read(zipBuffer, offset, zipBuffer.Length - offset);
                if (bytesRead <= 0)
                    break;
                offset += bytesRead;
            }
            if (resp.ContentEncoding == "gzip")
            {
                var ms = new MemoryStream(zipBuffer, 0, offset, true);
                var zipStream = new GZipStream(ms, CompressionMode.Decompress, true);
                ms.Seek(0, SeekOrigin.Begin);
                deBuffer = new byte[1024 * 1024];
                offset = 0;
                while (true)
                {
                    bytesRead = zipStream.Read(deBuffer, offset, 1024);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    offset += bytesRead;
                }
                zipStream.Close();
            }
            else
            {
                deBuffer = zipBuffer;
            }
            if (s != null) s.Close();
            resp.Close();
        }
        catch
        {
            if (s != null)
                s.Close();
            return null;
        }
        if (offset > 0)
        {
            var ms = new MemoryStream(deBuffer, 0, offset);
            deBuffer = ms.ToArray();
            ms.Close();
            return deBuffer;
        }
        return null;
    }

    public static string GetHtml(string url, string encode)
    {
        string content;
        try
        {
            var resq = GetRequest(url, "", "");
            var resp = (HttpWebResponse)resq.GetResponse();
            var ht = resp.GetResponseHeader("Content-Type");
            var buffer = HttpDecompress(resp);
            try
            {
                content = Encoding.GetEncoding("utf-8").GetString(buffer);
            }
            catch (ArgumentException)
            {
                content = Encoding.GetEncoding("gb2312").GetString(buffer);
            }
        }
        catch (Exception)
        {
            content = "";
        }
        return content;
    }
}